<?php
/**
 * 网站首页
 *
 * @category     Shuxun Cms
 * @date         2015年10月18日
 * @author       Shuxun Team <kaiping.jiang@scshux.com>
 * @file         IndexController.php
 */
namespace Scshux\Front\Controllers;
use Scshux\Utils\JSON;
use Scshux\Mvc\SModel;
use Scshux\Utils\Uploader;
use Scshux\Mvc\SQuery;
use Scshux\Plugin\Wechat;
use Scshux\Utils\Helper;
use Scshux\Tag\SmartyTag;
use Scshux\Utils\Tree;
use Scshux\Utils\Captcha;
use Scshux\Utils\Format;
class IndexController extends  ControllerBase
{

	/**
	 * 网站首页
	 */
	public  function  indexAction()
	{
		//新增语言过滤
		$site_id = $this->getCore()->getSiteID();

		//新闻中心
		$articleModel = new SModel('article');
		$article = $articleModel->query("flags='c'",'id,title,seo_description,created,pic','id','desc',2);
		$this->view->setVar('article',Format::TimeFormat($article,'created'));
		$this->view->setVar('site_id', $site_id);

		//产品分类
		$cateModel = new SModel('category');
		$cateList = $cateModel->query('parent_id=0','id,name,description,pic','sort desc,id','desc',3);
		$this->view->setVar('cateList',$cateList);

		//案例
		$article = $articleModel->query('cat_id=16','id,title,seo_description,created,pic','id','desc',12);
		$this->view->setVar('caselist',Format::TimeFormat($article,'created'));

	}


	/**
	 * 验证码
	 */
	public  function  captchaAction()
	{
	    $captchaObj = new Captcha();
	    $captchaObj->Generate();
	    $this->session->set('code',$captchaObj->getCode());
	    exit;

	}


	/**
	 * 装修咨询
	 */
	public function zixunAction()
	{
	    $output = array(
	            'code'=>0,
	            'msg'=>'错误的请求方式',
	    );
	    if (isset($_POST) && !empty($_POST) )
	    {
	        $userverify = $_POST['verify'];
	        $trueverfiy = $this->session->get('code');
	        if (strtoupper($userverify) != strtoupper($trueverfiy))
	        {
	            $output['msg'] = '验证码错误';
	        }
	        else
	        {
	            $zixunModel = new SModel('zxzixun');
	            unset($_POST['verify']);
	            $zixunModel->setData($_POST);
	            $bool = $zixunModel->add();
	            if ($bool)
	            {
	                $output['code'] = 1;
	            }
	            else
	            {
	                $output['msg'] = '咨询失败';
	            }
	        }
	    }
	    $this->sendJson($output);
	}


	/**
	 * 获取四个设计师
	 */
	private function getFourDesigner()
	{
	    $queryObj = new SQuery('designer');
	    $queryObj->fields = ' id,name,level,idea,year,head_ico ';
	    $queryObj->order = 'id desc';
	    $queryObj->limit = 4;
	    $designer = $queryObj->find();
	    $return  = array();
	    if ($designer)
	    {
	        $caseModel = new SQuery('cases');
	        $designId = array();
	        foreach ($designer as $k => $design)
	        {
	            $designId []= $design->id;
	            $return[$design->id] = $design;
	        }
	        $caseModel->where = 'design_id in('.join(',', $designId).')';
	        $caseModel->fields = 'count(*) as num,design_id';
	        $caseModel->group = ' design_id ';
	        $cases = $caseModel->find();
	        if ($cases)
	        {
	            foreach ($cases as $case)
	            {
	                $return[$case->design_id]->casenum = $case->num;
	            }
	        }
	    }
	    return $return;
	}


}